package com.zhs.gccs.view.gt;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


import com.zhs.gccs.R;
import com.zhs.gccs.model.GtLb;
import com.zhs.gccs.utils.OnNoDoubleClickListener;

import java.util.List;

/**
 * Created by 任伟伟
 * Datetime: 2016/11/18-11:07
 * Email: renweiwei@ufashion.com
 */

public class GtLbGroupAdapter extends RecyclerView.Adapter<GtLbGroupAdapter.ViewHolder> {
    private final Context mContext;
    private final List<GtLb> datas;


    public GtLbGroupAdapter(Context context, List<GtLb> datas) {
        this.datas = datas;
        this.mContext = context;
    }
    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_item_gtlb_group_item,viewGroup,false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, @SuppressLint("RecyclerView") final int position) {
        GtLb gtLb = datas.get(position);
        viewHolder.tv_title.setText(gtLb.getName());
        viewHolder.cb_selected.setChecked(gtLb.isChecked());
//        viewHolder.rv_list.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
//        GtLbItemAdapter itemAdapter = new GtLbItemAdapter(mContext, gtLb.getItems());
//        viewHolder.rv_list.setAdapter(itemAdapter);
//        itemAdapter.setOperationListener(new GtLbItemAdapter.OperationListener() {
//            @Override
//            public void onItemClick(int itemPosition) {
//                GtLbItem gtLbItem = gtLb.getItems().get(itemPosition);
//                gtLbItem.setChecked(!gtLbItem.isChecked());
//                itemAdapter.notifyItemChanged(itemPosition);
//            }
//        });
        viewHolder.ll_item.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
//                gtLb.setChecked(!gtLb.isChecked());
//                notifyItemChanged(position);
                if(operationListener!=null)operationListener.onItemClick(position);
            }
        });
    }
    //获取数据的数量
    @Override
    public int getItemCount() {
//        if(datas!=null&&datas.size()>3)return 3;
        return datas!=null?datas.size():0;
    }
    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tv_title;
        public View ll_item;
        public CheckBox cb_selected;
//        public RecyclerView rv_list;
        public ViewHolder(View view){
            super(view);
            tv_title =   view.findViewById(R.id.tv_title);
            ll_item =   view.findViewById(R.id.ll_item);
            cb_selected =   view.findViewById(R.id.cb_selected);
//            rv_list =   view.findViewById(R.id.rv_list);
        }
    }
    public interface OperationListener{
        void onItemClick(int position);
    }
    private OperationListener operationListener;

    public OperationListener getOperationListener() {
        return operationListener;
    }

    public void setOperationListener(OperationListener operationListener) {
        this.operationListener = operationListener;
    }
}
